package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.AppVersionMapper;
import com.weixing.mall.provider.model.domain.AppVersion;
import com.weixing.mall.provider.model.dto.cms.AppVersionDto;
import com.weixing.mall.provider.model.enums.cms.DeviceEunm;
import com.weixing.mall.provider.model.query.AppVersionQuery;
import com.weixing.mall.provider.model.vo.cms.AppVersionVo;
import com.weixing.mall.provider.service.IAppVersionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AppVersionServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/7/25
 * @Version 1.0
 **/
/**
 * <p>
 * CMS模块-APP版本控制表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements IAppVersionService {

    @Override
    public void save(AppVersionDto appVersionDto) {
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(appVersionDto, appVersion);
        appVersion.setDevice(appVersionDto.getDevice().getValue());
        appVersion.setIsForceUpdate(appVersionDto.getIsForceUpdate().getValue());
        appVersion.setIsPublish(appVersionDto.getIsPublish().getValue());

        if (null == appVersionDto.getId()) {
            save(appVersion);
        } else {
            appVersion.setUpdatedTime(new Date());
            updateById(appVersion);
        }
    }

    @Override
    public void deleteAll(int[] ids) {
        List list = new ArrayList();
        for(int i = 0;i < ids.length;i++){
            //未发布的才能删，取出来
            Integer a = getById(ids[i]).getIsPublish();
            if(a == 0){
                list.add(ids[i]);
            }
        }
        Object[] arr = list.toArray();
        int[] idss = new int[ids.length];
        for (int i = 0; i < arr.length; i++) {
            idss[i] = (int)arr[i];
        }
        baseMapper.delAll(idss);
    }

    @Override
    public IPage<AppVersionVo> selectOfPage(Page page, AppVersionQuery appVersionQuery) {
        return baseMapper.selectListOfPage(page,appVersionQuery);
    }

    @Override
    public void pubApp(AppVersionDto appVersionDto) {
        AppVersion appVersion = new AppVersion();
        BeanUtils.copyProperties(appVersionDto, appVersion);
        appVersion.setDevice(appVersionDto.getDevice().getValue());
        appVersion.setIsForceUpdate(appVersionDto.getIsForceUpdate().getValue());
        appVersion.setIsPublish(1);
        appVersion.setPublishTime(new Date());
        if (null == appVersionDto.getId()) {
            save(appVersion);
        } else {
            appVersion.setUpdatedTime(new Date());
            updateById(appVersion);
        }
    }
}
